﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Delta_util.Clases;

namespace Delta_editoresWeb.GridColumns
{
    public class ColumnaFoto : TemplateField
    {
        public string DataField { get; set; }

        public ColumnaFoto()
        {
            
            this.ItemTemplate = new ItemImagenTemplate(this);
        }
    }

    public class ItemImagenTemplate : ITemplate
    {
        protected ColumnaFoto datos;        
        Image lblinfo = null;
        Literal ltlink = null;
        public ItemImagenTemplate(ColumnaFoto padre) { datos = padre; }


        public void InstantiateIn(System.Web.UI.Control container)
        {
            ltlink = new Literal() { Text ="" };
            Literal ltlinkFinal = new Literal() { Text = "</a>" };
            lblinfo = new Image() { Width=100,Height=100,ImageAlign=ImageAlign.Middle};            
            lblinfo.DataBinding += OnDataBinding;
            ltlink.DataBinding += new EventHandler(ltlink_DataBinding);
            container.Controls.Add(ltlink);
            container.Controls.Add(lblinfo);
            container.Controls.Add(ltlinkFinal);
            
        }

        void ltlink_DataBinding(object sender, EventArgs e)
        {
            Literal ctrl = (Literal)sender;
            
        }

        private void OnDataBinding(object sender, EventArgs e)
        {
            Image ctrl = (Image)sender;
            string nombre_rel = "";
            object bound_value_obj = null;

            IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
            GridView grd = (data_item_container as GridViewRow).Parent.Parent as GridView;
            
            bound_value_obj = DataBinder.Eval(data_item_container.DataItem, datos.DataField);
            ctrl.ImageUrl = bound_value_obj.ToString();
            
            nombre_rel = "img_css_" + datos.DataField + "_" + data_item_container.DataItemIndex.ToString();
            ctrl.Page.Header.Controls.Add(Utilerias.CrearLinkCSS(ctrl.Page, this.GetType(), "Delta_editoresWeb.Plugins.css.colorbox.css"));
            ctrl.Page.ClientScript.RegisterClientScriptInclude(ctrl.Page.GetType(), "jcolorbox", ctrl.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Delta_editoresWeb.Plugins.js.jquery.colorbox-min.js"));
            
            Utilerias.ExecScript(ctrl.Page, "$('."+nombre_rel+"').colorbox({rel:'"+nombre_rel+"'});", "scriptcolumna", true);
            ltlink.Text = "<a class='"+nombre_rel+"' href='"+ ctrl.ImageUrl.Replace("~/",Utilerias.ObtenerHttpServer(ctrl.Page)) +"'  >";
            
            
        }

    }
}
